#! /usr/bin/env python
'''
    这是针对于各种情况的控制器集合
    该类要实现三个方法
    start(case,cmd)
    update(case,cmd)
    stop(case)
'''
# from multiprocessing import Queue,Process
from queue import Queue
from cases.manCase import ManCase
from cases.navCase import NavCase
from cases.wriCase import *
from cases.offCase import OffCase
from cases.onCase import *
from cases.tryCase import TryCase
import rospy
from command_handler.msg import manual_cmd

class CaseManager():

    def __init__(self):
        super().__init__()
        self.q = Queue(5)
        # self.caseList = {"man":ManCase(self.q),
        #                 "fcs":FcsCase(self.q),
        #                 "nav":NavCase(self.q),
        #                 "wri":WriCase(self.q),
        #                 "try":TryCase(self.q),
        #                 "on":OnCase(self.q),
        #                 "off":OffCase(self.q)
        #                 }
        self.start("off","default")

    def start(self,case,cmd):
        while(not self.q.empty()):
            #等待之前进程退出
            pass
        self.q = Queue(5)  #创建新的交换队列
        #将初始消息加入队列中
        self.q.put(cmd)
        #开启整个进程
        self.getObject(case).start()


    def stop(self,case):
        #结束整个进程，之后新建一个进程作为补充
        self.q.put("quit")
        # self.caseList[case] = self.getObject(case)
        # print("新建进程"+case)

    def update(self,cmd):
        #这里将信息写入队列
        if(self.q == None):
            print("[warning]:进程尚未开启，请等待")
            return
        self.q.put(cmd)
    
    def getObject(self,case):
        if(case == "man" or case == "fcs"):
            return ManCase(self.q)
        elif(case ==  "try"):
            return TryCase(self.q)
        elif(case == "wri"):
            return WriCase(self.q)
        elif(case == "on"):
            return OnCase(self.q)
        elif(case == "off"):
            return OffCase(self.q)
        elif(case == "nav"):
            return NavCase(self.q)



